package edu.hawaii.halealohacli.jcev.command;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * JUnit test cases for MonitorPower.
 * 
 * @author Micah
 *
 */
public class TestMonitorPower {
  
  
  private WattDepotClient goodClient = 
      new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/");
  private WattDepotClient badClient = 
      new WattDepotClient("Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn.");
  private String errorMessage = "Error: Exception message does not match expected value.";
  private String unexpectedError = "Error: Good input not accepted properly";
  
  /**
   *  Constructor.
   */
  public TestMonitorPower() {
    // No need to do anything here.
  }
  
  /**
   * Checks for proper error handling when bad values are passed in.
   */
  @Test
  public void testInputChecking() {
    
    MonitorPower monitorPower = new MonitorPower();
    monitorPower.setTestMode(true);
    
    try {
      monitorPower.execute(null, null);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage,
          e.getMessage().equals("Error: Cannot pass null client."));
    }
    
    try {
      monitorPower.execute(null, this.badClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Problem querying data from client. " +
          "Ensure that WattDepot client is valid and internet connection is availible."));
    }
    
    try {
      monitorPower.execute(null, this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input String null."));
    }
    
    try {
      monitorPower.execute(" ", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input contains incorrect number of arguments."));
    }
    
    try {
      monitorPower.execute("One Two Three", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input contains incorrect number of arguments."));
    }
    
    try {
      monitorPower.execute("Source", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input source is invalid."));
    }
    
    try {
      monitorPower.execute("Lokelani NotAnInt", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input interval is not valid."));
    }
    
    try {
      monitorPower.execute("Lokelani", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals(this.unexpectedError));
    }
    
    try {
      monitorPower.execute("Lokelani 5", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals(this.unexpectedError));
    }
    
  }
  
  /**
   * Tests the behavior of the MonitorPowerTask helper class in MonitorPower.java.
   */
  @Test
  public void testMonitorTask() {
    
    MonitorPowerTask task = null;
    
    try {
      task = new MonitorPowerTask(null, null);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Cannot pass null client."));
    }
    
    try {
      task = new MonitorPowerTask(null, this.badClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Problem querying data from client. " +
          "Ensure that WattDepot client is valid and internet connection is availible."));
    }
    
    try {
      task = new MonitorPowerTask(null, this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input String null."));
    }
    
    try {
      task = new MonitorPowerTask("InvalidSource", this.goodClient);
    }
    catch (Exception e) {
      assertTrue(this.errorMessage, 
          e.getMessage().equals("Error: Input source is invalid."));
    }
    
    try {
      task = new MonitorPowerTask("Lokelani", this.goodClient);
      task.run();
    }
    catch (Exception e) {
      fail(this.unexpectedError);
    }
  }
  
  
}
